import page
from base.base import Base
from tool.get_log import GetLogger

log = GetLogger.get_log()


class PageLogin(Base):

    # 点击我的按钮
    def pagelogin_click_mine(self):
        self.base_click_element(page.wd_tab)

    # 调起一键登录
    def pagelogin_call_login(self):
        self.base_click_element(page.wd_login)

    # 点击登录按钮
    def pagelogin_click_otherstyle(self):
        self.base_click_element(page.login_otherstyle)

    # 点击账号密码登录
    def pagelogin_click_namepwd(self):
        self.base_click_element(page.login_namepwd)

    # 输入用户名
    def pagelogin_input_username(self, username='11100009001'):
        self.base_input_value(page.login_username, username)

    # 输入密码
    def pagelogin_input_password(self, password='123456'):
        self.base_input_value(page.login_password, password)

    # 点击提交
    def pagelogin_click_submit(self):
        self.base_click_element(page.login_submit)

    def pagelogin_logout(self):
        self.base_click_element(page.wd_setting)
        self.base_click_element(page.setting_logout)
        self.base_click_element(page.setting_logout_confirm)

    def pagelogin_close_login(self):
        try:
            # self.base_click_element(page.logIn_back_btn
            el = self.base_find_element(page.logIn_back_btn,timeout=5)
            el.click()
        except:
            try:
                el = self.base_find_element(page.login_back_no_phone,timeout=5)
                el.click()
            except:
                pass
        #     self.pagelogin_return_to_wsy_keycode()
        # finally:
        #     raise Exception('打开弹出登录页，跳过失败')

    def pagelogin_return_to_wsy_keycode(self):
        for a in range(10):
            log.info(f'正在返回设置页面，第{a + 1}次尝试')
            if self.base_if_element_exist(page.glsx_tab):
                break
            else:
                self.driver.press_keycode(4)
        self.base_click_element(page.wsy_tab)

    def pagelogin_click_select(self):
        self.base_click_element(page.login_select_frame)

    def pagelogin_close_first_page_tanchuang(self):
        log.info("正在查找并关闭首页弹窗")
        while not self.base_if_element_exist(page.wd_tab):
            log.info("找到弹窗，点击关闭按钮")
            self.base_click_element(page.general_tanchuang_close_btn)
        log.info("弹窗已全部关闭，已经进入首页")

    # 业务组合方法
    def pagelogin(self,username,password):
        log.info("调用组合业务方法")
        try:
            self.pagelogin_close_login()
        except:
            pass
        self.pagelogin_close_first_page_tanchuang()
        self.pagelogin_click_mine()
        self.pagelogin_call_login()
        try:
            self.pagelogin_click_otherstyle()
        except:
            pass
        self.pagelogin_click_namepwd()
        self.pagelogin_input_username(username)
        self.pagelogin_input_password(password)
        try:
            self.pagelogin_click_select()
        except:
            pass
        self.pagelogin_click_submit()
